<?php
namespace app\admin\controller;

use app\common\model\Upgrade as UpgradeModel;
use app\common\controller\AdminBase;
use app\common\model\Upgrade;
use think\Config;
use think\Db;

/**
 * 审核认证
 * Class AdminUser
 * @package app\admin\controller
 */
class Uplevel extends AdminBase
{

    protected $upgrade_model;
    protected function _initialize()
    {
        parent::_initialize();
        $this->upgrade_model = new UpgradeModel();
    }

    /**
     * 订单管理 index
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function index($keyword = '', $page = 1)
    {
//        $map = [];
//        if ($keyword) {
//            $map['username|mobile|email'] = ['like', "%{$keyword}%"];
//        }
        $map['a.status'] = 0;
        $map['a.is_pingtai'] = 1;
        $order_list = $this->upgrade_model->alias('a')->join('user b','a.user_id=b.id')->where($map)->field('a.*,b.username,b.mobile')->order('a.id DESC')->paginate(15, false, ['page' => $page]);
        foreach ($order_list as $k=>$v){
            $order_list[$k]['up_username'] = Db::name('user')->where('id',$v['up_user_id'])->value('username');
            $order_list[$k]['add_time'] = date('Y-m-d H:i:s');
            $order_list[$k]['image'] = explode(',',$v['image']);

        }
        return $this->fetch('index', ['order_list' => $order_list, 'keyword' => $keyword,'img_url'=>'http://'.$_SERVER['SERVER_NAME'].'/public/']);
    }
    /**
     * 订单管理 all
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function all($keyword = '', $page = 1)
    {
        $map['a.is_pingtai'] = 1;
        $order_list = $this->upgrade_model->alias('a')->join('user b','a.user_id=b.id')->where($map)->field('a.*,b.username,b.mobile')->order('a.id DESC')->paginate(15, false, ['page' => $page]);
        foreach ($order_list as $k=>$v){
            $order_list[$k]['up_username'] = Db::name('user')->where('id',$v['up_user_id'])->value('username');
        }
        return $this->fetch('all', ['order_list' => $order_list, 'keyword' => $keyword]);
    }

    //审核
    public function shenhe($id,$status){
        $s = Db::name('upgrade')->where('id',$id)->find();
        if(!$s){
            return json(['Status'=>0,'Message'=>'数据错误']);
        }
        $is_upgrade = 0;
        $save['pingtai_time'] = time();
        if($status==1){
            $save['pingtai_status'] = 1;
            if($s['up_status']==1){
                $is_upgrade = 1;
                $save['status'] = 1;
                $save['status_time'] = time();
            }elseif($s['up_status']==2){
                $save['status'] = 2;
                $save['status_time'] = time();
            }
        }else{
            $save['pingtai_status'] = 2;
            $save['status'] = 2;
            $save['status_time'] = time();
        }
        $y = Db::name('upgrade')->where('id',$id)->update($save);
        if($y){
            if ($is_upgrade==1){
                $level = $s['level'] +1;
                Db::name('user')->where(['id'=>$s['user_id']])->update(['level'=>$level]);
            }
            return json(['Status'=>1,'Message'=>'保存成功']);
        }else{
            return json(['Status'=>0,'Message'=>'保存失败，请稍后再试']);
        }
    }
}